package cogbio.polFileGenerator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Utils { 
	
	public static String extractHeader(String input){
		File file = new File(input);
		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String temp = null;
			while ( (temp = br.readLine()) != null){
				//temp = br.readLine();
				if (temp.contains("[Trials]")){
					break;
				}
				sb.append(temp+"\n");
				
			}
			br.close();
			//System.out.println(sb.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		String buffer = sb.toString();
		if (buffer.trim().length()==0){
			System.out.println("no header in the file	");
			return "empty";
		}
		
		return sb.toString();
	}

    // swaps array elements i and j
    public static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }

    // take as input an array of strings and rearrange them in random order
    public static void shuffle(Object[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++) {
            int r = i + (int) (Math.random() * (N-i));   // between i and N-1
            exch(a, i, r);
        }
    }
 // take as input an array of strings and rearrange them in random order
    public static void reshuffle(Object[] a) {
        int N = a.length;
        for (int i = 1; i < N; i++) {
            int r = i + (int) (Math.random() * (N-i));   // between i and N-1
            exch(a, i, r);
        }
    }
    // take as input an array of strings and print them out to standard output
    public static void show(String[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
    public static void getFirstPositive(String[] a, List<String> posMask, int sessionSize){
    	
    	for (int j=0;j<a.length;j++){
    		
    		
    		if (j%sessionSize==0 && !posMask.contains(a[j])){
    			//System.out.println(a[j]+" is not positive");
    			for (int i=1;i<a.length;i++){
    				if (posMask.contains(a[i])){
    					exch(a,j,i);
    					break;
    				}
    			}
    		}
    	}
    }
    public static void removeClusters(String[] a, List<String> posMask, List<String> negMask, int limit){
    	int clusterSize = 0;
    	boolean clusterPositive = true;
    	for (int i=0;i<a.length;i++){
    		//System.out.println(i);
    		if(posMask.contains(a[i])&& clusterPositive){
    			clusterSize++;
    		} else if (posMask.contains(a[i])&& !clusterPositive){
    			clusterSize = 1;
    			clusterPositive = true;
    		} else if(negMask.contains(a[i])&& !clusterPositive){
    			clusterSize++;
    		} else if (negMask.contains(a[i])&& clusterPositive){
    			clusterSize = 1;
    			clusterPositive = false;
    		}
    		//System.out.println(a[i]);
    		//System.out.println(clusterSize);
    		if (clusterSize>limit) {
    			//System.out.println("reached cluster");
    			int security = 0;
    			for (;;){
    				
    				//+1 never change the first in order
    				int j = (int) (Math.random()*(a.length-i-1))+i;
    				//System.out.println(j);
    				if (clusterPositive){
    					if (negMask.contains(a[j])){
    						exch(a, i, j);
    						break;
    					}
    				}
    				
    				if (!clusterPositive){
    					if (posMask.contains(a[j])){
    						exch(a, i, j);
    						break;
    					}
    				}
    				if (security++>100){
    					System.out.println("could not remove cluster, reshuffling");
    					reshuffle(a);
    					i=0;
    					break;
    				}
    			}
    			if (!(security>100)){
    				clusterSize = 1;
    				clusterPositive = !clusterPositive;
    			} else {
    				clusterSize = 0;
    		    	
    			}
    		}
    	}
    	
    }

	public static void pushGeneralizationFiles(String[] internalFiles,
			String[] aClass, String[] bClass, int genAmount) {
		
		int clusterSize = internalFiles.length/genAmount;
		for (int i =0;i<genAmount;i++){
			int position = (int) ((Math.random() * (clusterSize-1))+1);
			if (i%2==0){
				//System.out.println("Exchanged "+internalFiles[5*i+position]+" for "+aClass[i/2]);
				internalFiles[clusterSize*i+position] = aClass[i/2];
			}
			if (i%2==1){

				//System.out.println("Exchanged "+internalFiles[5*i+position]+" for "+bClass[i/2]);
				internalFiles[clusterSize*i+position] = bClass[i/2];
			}
		}
	}

	public static List<String> shuffle(List<String> aClass) {
		List<String> returnArray = new ArrayList<String>();
		Object[] array = aClass.toArray();
		shuffle(array);
		for (Object current: array){
			returnArray.add(current.toString());
		}
		return returnArray;
	}

    
}